﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FlyingClub.Models;
using FlyingClub.Models.ViewsModels;

namespace FlyingClub.Controllers
{ 
    public class PlanesController : BeforeController
    {
        private FlyingClubDBEntities db = new FlyingClubDBEntities();

        //
        // GET: /Planes/

        public ActionResult Index()
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");
            else if (_access == 1)
                return RedirectToAction("AccessError", "Home");
            else
                return View(db.Plane.ToList());
        }

        //
        // GET: /Planes/Details/5

        public ActionResult Details(int id)
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");
            else if (_access == 1)
                return RedirectToAction("AccessError", "Home");
            else
            {
                PlanesDetailsViewModel view = new PlanesDetailsViewModel();
                view.Plane = db.Plane.Single(p => p.ID == id);

                view.CalendarMaxDay = new List<DateTime>();
                for(int i = 0; i < 12; i++)
                {

                    int Year = DateTime.Now.Year;
                    if (DateTime.Now.Month + i > 12)
                        Year += 1;
                    int Month = ((DateTime.Now.Month -1 + i) % 12)+1;
                    
                    DateTime Date = new DateTime(Year, Month, 1);
                    view.CalendarMaxDay.Add(Date);
                }
                return View(view);
            }
        }

        //
        // GET: /Planes/Create

        public ActionResult Create()
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");
            else if (_access == 1)
                return RedirectToAction("AccessError", "Home");
            else
                return View();
        } 

        //
        // POST: /Planes/Create

        [HttpPost]
        public ActionResult Create(PlaneCreateViewModel plane)
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");
            else if (_access == 1)
                return RedirectToAction("AccessError", "Home");
            else
            {
                if(plane.Name != null && plane.Number != null && plane.HourRate != 0)
                {
                    Plane p = new Plane();
                    p.Number = plane.Number;
                    p.Name = plane.Name;
                    p.HourRate = plane.HourRate;

                    db.Plane.AddObject(p);
                    db.SaveChanges();

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(plane);
                }
            }
        }
        
        //
        // GET: /Planes/Edit/5
 
        public ActionResult Edit(int id)
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");
            else if (_access == 1)
                return RedirectToAction("AccessError", "Home");
            else
            {
                Plane plane = db.Plane.Single(p => p.ID == id);

                PlaneCreateViewModel planeVM = new PlaneCreateViewModel();
                planeVM.ID = plane.ID;
                planeVM.HourRate = plane.HourRate;
                planeVM.Name = plane.Name;
                planeVM.Number = plane.Number;

                return View(planeVM);
            }
        }

        //
        // POST: /Planes/Edit/5

        [HttpPost]
        public ActionResult Edit(PlaneCreateViewModel plane)
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");
            else if (_access == 1)
                return RedirectToAction("AccessError", "Home");
            else
            {
                if (plane.Name != null && plane.Number != null && plane.HourRate != 0)
                {
                    Plane p = new Plane();
                    p.ID = plane.ID;
                    p.Number = plane.Number;
                    p.Name = plane.Name;
                    p.HourRate = plane.HourRate;

                    db.Plane.Attach(p);
                    db.ObjectStateManager.ChangeObjectState(p, EntityState.Modified);
                    db.SaveChanges();

                    return RedirectToAction("Index");
                }
                else
                {
                    return View(plane);
                }
            }
        }

        //
        // GET: /Planes/Delete/5
 
        public ActionResult Delete(int id)
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");
            else if (_access == 1)
                return RedirectToAction("AccessError", "Home");
            else
            {
                Plane planes = db.Plane.Single(p => p.ID == id);
                return View(planes);
            }
        }

        //
        // POST: /Planes/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            int _access = this.GetAccess();

            if (_access == 0)
                return RedirectToAction("LogOn", "Members");
            else if (_access == 1)
                return RedirectToAction("AccessError", "Home");
            else
            {
                Plane planes = db.Plane.Single(p => p.ID == id);
                db.Plane.DeleteObject(planes);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult Calendar(int planeID, int year, int month, int day)
        {
            int _access = this.GetAccess();

            if (_access == 1 || _access == 2)
            {
                // Parser toute la journée, heure par heure, et vérifier qu'il n'y a pas de vol réservé
                List<int> result = new List<int>();
                DateTime Date = new DateTime(year, month, day, 0, 0, 0);

                for (int i = 0; i < 24; i++)
                {
                    if (this.PlaneIsAvailable(planeID,new DateTime(year, month, day, i, 0, 0)))
                    {
                        result.Add(0);
                    }
                    else
                    {
                        result.Add(1);
                    }
                }

                return View(result);
            }
            return RedirectToAction("LogOn", "Members");
        }
    }
}